package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.dto.shop.ShopQualificationAuditDTO;
import com.ztgf.shopping.models.dto.shop.ShopQualificationDTO;
import com.ztgf.shopping.models.service.shop.IShopQualificationAuditService;
import com.ztgf.shopping.models.service.shop.IShopQualificationService;
import com.ztgf.shopping.models.vo.shop.ShopQualificationBrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 店铺资质表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:40:54
 */

@RestController
@RequestMapping("/api/shopQualification")
@Slf4j
@Api(value = "ShopQualificationApiController",tags = "店铺资质表Api接口")
public class ShopQualificationApiController {

    @DubboReference
    private IShopQualificationService shopQualificationService;
    @DubboReference
    private IShopQualificationAuditService iShopQualificationAuditService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopQualificationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        ShopQualificationDTO dto = shopQualificationService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/getLicenseByUserId")
    @ApiOperation(value = "根据用户ID查找营业执照",notes = "根据用户ID查找营业执照")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopQualificationAuditDTO> getLicenseByUserId(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        //type 审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目
        Integer auditType = 0;
        //类型 0营业执照，1法人身份证，3开户许可证，4商标注册证书，5续展证明文件，6ISO认证书，7品牌授权书
        Integer type = 0;
        ShopQualificationAuditDTO dto = iShopQualificationAuditService.getByUserId(id, type, auditType);
        return new Result<>(dto);
    }

    @PostMapping("/getLicense")
    @ApiOperation(value = "根据店铺id查找营业执照",notes = "根据店铺id查找营业执照")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopQualificationDTO> getLicense(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();
        //类型 0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        wrapper.eq("shop_id", id).eq("type", 0);
        ShopQualificationDTO dto = shopQualificationService.selectOne(wrapper);
        return new Result<>(dto);
    }

    @ApiOperation(value = "店铺基础资质表列表",notes = "店铺基础资质表列表")
    @PostMapping("/queryBaseList")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShopQualificationDTO>> queryBaseList(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", id);
        //类型 0营业执照，1法人身份证，2开户许可证，3商标注册证书，4续展证明文件，5ISO认证书，6品牌授权书
        wrapper.andNew().eq("type", 0).or().eq("type", 1).or().eq("type",2);

        List<ShopQualificationDTO> shopQualificationDTOList = shopQualificationService.selectList(wrapper);

        return new Result<>(shopQualificationDTOList);
    }

    @ApiOperation(value = "店铺品牌资质表列表",notes = "店铺品牌资质表列表")
    @PostMapping("/queryBrandList")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShopQualificationBrandVO>> queryBrandList(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        List<ShopQualificationBrandVO> vo = shopQualificationService.queryBrandList(id);

        return new Result<>(vo);
    }
}
